package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.result.UserLoginMsg;
import com.cskaoyan.mall.commons.util.GetUserInfoUtils;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * @Author: 浩澄
 * @Date: 2023/1/10-10:20
 * @Mission: 商品模块
 */
@RequestMapping("shopping")
@RestController
public class ShoppingController {

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IProductCateService iProductCateService;

    @Autowired
    private ICartService iCartService;

    @Resource
    private IContentService iContentService;
    @Resource
    private IHomeService iHomeService;

    @GetMapping("recommend")
    public ResponseData getRecommendGoods() {

        Set<PanelDto> recommendGoods = iProductService.getRecommendGoods();

        return new ResponseUtil<>().setData(recommendGoods);
    }

    @GetMapping("navigation")
    public ResponseData navigation() {
        List<PanelContentDto> panelContentDtos = iContentService.queryNavList();
        return new ResponseUtil<>().setData(panelContentDtos);
    }

    /**
     * 获取购物车列表
     *
     * @param request
     * @return
     */
    @GetMapping("carts")
    public ResponseData getCartListById(HttpServletRequest request) {

        UserLoginMsg userLoginMsg = GetUserInfoUtils.getUserInfo(request);
        String userId = String.valueOf(userLoginMsg.getUid());
        String username = userLoginMsg.getUsername();
        // redis中用户购物车信息的key --> username + userId
        String userCartKey = username + userId;

        // 根据用户id获取用户的购物车商品列表
        List<CartProductDto> cartList = iCartService.getCartListById(userCartKey);

        ResponseUtil<List<CartProductDto>> responseUtil = new ResponseUtil<>();
        ResponseData<List<CartProductDto>> responseData = responseUtil.setData(cartList);

        return responseData;
    }

    /**
     * 全选/不选购物车商品
     * @param checkAllItemRequest
     * @param httpServletRequest
     * @return
     */
    @PutMapping("items")
    public ResponseData checkedAllItem(@RequestBody CheckAllItemRequest checkAllItemRequest, HttpServletRequest httpServletRequest) {
        UserLoginMsg userLoginMsg = GetUserInfoUtils.getUserInfo(httpServletRequest);
        String username = userLoginMsg.getUsername();
        Long userId = userLoginMsg.getUid();
        String userCartKey = username + userId;
        iCartService.checkAllCartItem(checkAllItemRequest, userCartKey);
        return new ResponseUtil<>().setData("成功", "666");
    }

    /**
     * 分页查询商品列表
     * @param pageInfo
     * @return
     */
    @GetMapping("goods")
    public ResponseData getShoppingGoods(PageRequest pageInfo) {
        AllProductResponse allProductResponse = iProductService.getAllProduct(pageInfo);
        return new ResponseUtil<>().setData(allProductResponse, "666");
    }

    /**
     * 列举所有商品种类
     * @return
     */
    @GetMapping("categories")
    public ResponseData getAllProductCate(){
        List<ProductCateDto> productCateDtos = iProductCateService.getAllProductCate("1");
        return new ResponseUtil<>().setData(productCateDtos, "666");
    }

    /**
     * 更新购物车中的商品
     *
     * @param cartRequest
     * @param request
     * @return
     */
    @PutMapping("carts")
    public ResponseData updateCartNum(@RequestBody CartRequest cartRequest, HttpServletRequest request) {

        iCartService.updateCartNum(cartRequest, request);

        return new ResponseUtil<>().setData("成功");
    }

    /**
     * 删除购物车中选中的商品
     *
     * @param userId
     * @param request
     * @return
     */
    @DeleteMapping("items/{uid}")
    public ResponseData deleteCheckedItem(@PathVariable("uid") Long userId, HttpServletRequest request) {

        iCartService.deleteCheckedItem(userId, request);

        return new ResponseUtil<>().setData("成功");
    }

    @PostMapping("carts")
    public ResponseData addToCart(@RequestBody CartRequest cartRequest, HttpServletRequest httpServletRequest) {

        //验证用户id是否与post中的一致
        Cookie[] cookies = httpServletRequest.getCookies();
        System.out.println("cookies = " + cookies);
        UserLoginMsg userInfo = GetUserInfoUtils.getUserInfo(httpServletRequest);
        String username = userInfo.getUsername();
        Long uid = userInfo.getUid();

        //对比userid
        if (cartRequest.getUserId() != uid) {

            throw new ValidateException(SysRetCodeConstants.TOKEN_VALID_FAILED.getCode(), SysRetCodeConstants.TOKEN_VALID_FAILED.getMessage());

        } else {

            iCartService.addToCart(cartRequest, username);
            return new ResponseUtil<>().setData("成功");

        }

    }

    /**
     * 商品详情
     *
     * @param id
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author 杨宇健
     * @since 2023/01/10 14:08
     */
    @GetMapping("product/{id}")
    public ResponseData productById(@PathVariable("id") Long id) {
        ProductDetailDto productDetail = iProductService.getProductDetail(id);
        return new ResponseUtil<>().setData(productDetail);
    }

    /**
     * homePage
     *
     * @author xD
     * @since 2023/01/10 16:14
     */
    @GetMapping("homepage")
    public ResponseData homepage() {
        Set<PanelDto> panelDto = iHomeService.homepage();
        return new ResponseUtil<>().setData(panelDto);
    }

    /**
     * 删除购物车中的商品
     *
     * @author 张舒鹏
     * @param uid    用户id
     * @param itemId 商品id
     * @return
     */
    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable("uid") Long uid,@PathVariable("pid")Long itemId, HttpServletRequest request) {

        UserLoginMsg userInfo = GetUserInfoUtils.getUserInfo(request);

        iCartService.deleteCartItem(uid, itemId, userInfo);

        return new ResponseUtil<>().setData("成功");

    }

    /**
     * @author 张舒鹏
     */

}
